﻿using System.Web;

using Newtonsoft.Json;
using RaceList.activity.help;

namespace RaceList.activity.relay
{
    /// <summary>
    ///     Handler1 的摘要说明
    /// </summary>
    public class Award : IHttpHandler
    {
        public HttpResponse Response { get; set; }

        public void ProcessRequest(HttpContext context)
        {
            Response = context.Response;
            string method = context.Request.Form["m"];
            string code = context.Request.Form["code"];
            switch (method)
            {
                case "CheckCode":
                    CheckCode(code);
                    break;
            }
        }

        public bool IsReusable
        {
            get { return false; }
        }

        private void WriteToClient(object o)
        {
            Response.Write(JsonConvert.SerializeObject(o));
        }

        public void CheckCode(string code)
        {
            Activity_AwardUser awardUser = RelayQuery.GetActivity_AwardUser(code);
            if (awardUser == null)
            {
                WriteToClient(new {status = 500, message = "兑奖码无效"});
                return;
            }
            if (awardUser.isAward)
            {
                WriteToClient(new {status = 500, message = "兑奖码已使用，请耐心等待工作人员联系"});
                return;
            }
            WriteToClient(new {status = 200, awardUser.name, awardUser.phone});
        }
    }
}